package com.sto.stoapp.db.manager;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.StoTableFieldName;
import com.sto.stoapp.entity.PictureInfoEntity;
import com.sto.stoapp.entity.TableFieldInfoEntity;
import com.sto.stoapp.util.UnifiedStaticType;

public class PictureQueryManager extends StoDataBase {

	public PictureQueryManager(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 搜索数据(用于获取下载列表数据)
	 * 
	 */
	public List<PictureInfoEntity> queryAll(Context context,
			TableFieldInfoEntity entity) throws Exception{

		SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);

		List<PictureInfoEntity> picEntity = selectQueryData(db.rawQuery(
				entity.getSelectSql(), entity.getSelectField()));
		closeDb(UnifiedStaticType.WRITE_DATABASE);
		return picEntity;
	}

	/**
	 * 通过游标获取数据
	 * 
	 * @return
	 */
	@SuppressLint("NewApi")
	private List<PictureInfoEntity> selectQueryData(Cursor c) {
		List<PictureInfoEntity> listData = new ArrayList<PictureInfoEntity>();
		try {
			if (c != null) {
				StoTableFieldName stoTableFieldName = new StoTableFieldName();

				if (c.moveToFirst()) {
					do {
						PictureInfoEntity data = new PictureInfoEntity();
						// 图片类型
						data.setPictureType(c.getString(c
								.getColumnIndex(stoTableFieldName.COMMAND_TYPE)));
						// 图片单号
						data.setPictureCode(c.getString(c
								.getColumnIndex(stoTableFieldName.BAR_CODE_NUMBER)));
						// 图片日期
						data.setPictureDate(c.getString(c
								.getColumnIndex(stoTableFieldName.SCAN_DATE)));
						// 图片名称
						data.setPictureName(c.getString(c
								.getColumnIndex(stoTableFieldName.USER_DEFINED4)));

						// 图片上传状态
						data.setUploadStatus(c.getString(c
								.getColumnIndex(stoTableFieldName.SEND_STATE)));
						listData.add(data);
					} while (c.moveToNext());
				}
				closeCursor(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return listData;
	}
}
